package com.flyan.game.motafx.core.map;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 塔地图
 *
 * @author flyan
 * @version 1.0
 * @email flyan_by_chenu@sina.com
 * @since 2025/2/1
 */
@Data
public class TowerMap {

    /**
     * 楼层列表
     */
    private Map<Integer, FloorMap> tower;

    /**
     * 当前楼层号
     */
    private int currentFloorNumber;

    public FloorMap getCurrentFloor() {
        return tower.get(currentFloorNumber);
    }

    /**
     * 获取楼层
     *
     * @param floorNumber 楼层号
     * @return 楼层
     */
    public FloorMap getFloor(int floorNumber) {
        return tower.get(floorNumber);
    }

    /**
     * 切换楼层
     *
     * @param floorNumber 楼层号
     */
    public void changeFloor(int floorNumber, MapPosition bravePosition) {
        if (floorNumber == currentFloorNumber || !tower.containsKey(floorNumber)) {
            return;
        }

        FloorMap currentFloor = getCurrentFloor();
        currentFloor.braveLeave(); // 勇士离开当前楼层
        currentFloor.clearEntityMap(); // 清理该楼层的实体

        this.currentFloorNumber = floorNumber; // 记录新楼层
        currentFloor = getCurrentFloor();
        currentFloor.braveEnter(bravePosition); // 进入新楼层
        drawMap(); // 渲染新楼层地图
    }

    /**
     * 上楼
     */
    public void upFloor() {
        if (tower.containsKey(currentFloorNumber + 1)) {
            FloorMap currentFloor = getCurrentFloor();
            changeFloor(currentFloorNumber + 1, currentFloor.getBravePosition());
        }
    }

    /**
     * 下楼
     */
    public void downFloor() {
        if (tower.containsKey(currentFloorNumber - 1)) {
            FloorMap currentFloor = getCurrentFloor();
            changeFloor(currentFloorNumber - 1, currentFloor.getBravePosition());
        }
    }

    /**
     * 绘制地图
     */
    public void drawMap() {
        getCurrentFloor().drawMap();
    }

    /**
     * 添加楼层
     *
     * @param floor 楼层
     */
    public void addFloor(FloorMap floor) {
        if (tower == null) {
            tower = new HashMap<>();
        }
        tower.put(floor.getFloorInfo().getNumber(), floor);
    }

}
